import requests
import utility
import os
import config
import concurrent.futures
import workQueue
import workItem

#线程池
executor = concurrent.futures.ThreadPoolExecutor(max_workers=None)

def getNoticeInfo(companyCode,pageIndex):
    p={
        'callback':'callFun',
        'page':pageIndex,
        'disclosureType':5,
        'companyCd':companyCode,
        'isNewThree':1,
        'startTime':'',
        'endTime':'',
        'keyword':'',
        'xxfcbj':''
    }
    baseUrl = 'http://www.neeq.com.cn/disclosureInfoController/infoResult.do'
    res = requests.get(baseUrl,params=p)
    noticeObj = utility.parseResponseBody(res.text)
    listInfo = noticeObj['listInfo']
    #如果是第一个分页
    if(listInfo['firstPage'] and len(listInfo['content']) > 0):
        msg = "{} {} 总共有 {} 条数据，{} 个分页".format(companyCode,listInfo['content'][0]['companyName'], listInfo['totalElements'], listInfo['totalPages'])
        print(msg)
        try:
            #目录
            localDir = os.path.join(config.basePath,"{}_{}".format(listInfo['content'][0]['companyCd'],listInfo['content'][0]['companyName']))
            #如果目录不存在，就创建
            if not os.path.exists(localDir):
                os.mkdir(localDir)
            readmeFilePath = os.path.join(localDir,'readme.txt')
            with open(readmeFilePath,'w') as f:
                f.write(msg)
        except Exception as ex:
            print("\n异常 保存文件[{}]出现异常，异常信息：{}".format(readmeFilePath,ex))
    
    #下载PDF文档
    downloadNoticeDoc(listInfo)
    #页数加1
    pageIndex += 1
    if(pageIndex < listInfo['totalPages']):
        #继续请求公告列表
        getNoticeInfo(companyCode,pageIndex)

def downloadFile(url,filePath,order,fileName):
    try:
        res = requests.get(url,timeout=60)
        #保存文件
        with open(filePath,'wb') as f:
            f.write(res.content)
            print("\n下载 [{}] [{}] {} 完成".format(order,len(res.content),fileName))
    except Exception as ex:
        print("\n异常 [{}] {} {}".format(order,fileName,ex))
    else:
        return

def downloadNoticeDoc(listInfo):
    if(listInfo['totalElements'] < 1):
        print("没有数据")
        return
    orderPre = listInfo['number']*listInfo['size']
    i = 0
   
    for notice in listInfo['content']:
        companyName = notice['companyName']
        destFilePath = notice['destFilePath']
        destFileName = notice['disclosureTitle']
        destFileExt = notice['fileExt']
        publishDate = notice['publishDate']
        companyCd = notice['companyCd']
        order = orderPre + i
        orderStr = "[{}] ".format(order)
        i+=1

        #目录
        localDir = os.path.join(config.basePath,"{}_{}".format(companyCd,companyName))
        #如果目录不存在，就创建
        if not os.path.exists(localDir):
            os.mkdir(localDir)
        #构造文件路径
        fileName = utility.getFileName("{}_{}_{}".format(publishDate,order,destFileName),destFileExt)
        #绝对文件路径
        localFilePath = os.path.join(localDir,fileName)

        #下载文件
        #print("{}准备下载 {}".format(orderStr,fileName))
        p={}
        url = 'http://www.neeq.com.cn{0}'.format(destFilePath)

        #使用线程池下载
        #executor.submit(downloadFile,url,localFilePath,order,fileName)
        #直接下载
        #downloadFile(url,localFilePath,order,fileName)
        #使用队列下载
        workQueue.addWorkItem(workItem.WorkItem(url,localFilePath,order,fileName))

class DownloadMgr(object):
    def __init__(self):
        self.itemList = {}
        self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=None)
        

    def start(self):
        pass

    def addItem(self,item):
        self.itemList[item.url] = item
        self.executor.submit()

class DownloadItem(object):
    def __init__(self,url,fielPath,order,fileName):
        self.url = url
        self.filePath
        self.order
        self.fileName
        self.count = 0
        self.isSuccess = False

    def addCount(self):
        self.count += 1
        
  
#入口
#if __name__ == '__main__':
#    config.basePath = os.path.join(os.path.abspath('.'),'download_file')
#    if(not os.path.exists(config.basePath)):
#        os.mkdir(config.basePath)
#    getNoticeInfo('831266',0)
